package com.zx.platform.verifycode;

import javax.servlet.http.HttpSession;


/**
 * helper
 *
 * @author Qizhan Lu
 * @version 1.0
 */
public class VerifyCodeHelper
{
    public static final String VERIFYCODE_BINDKEY = "_verifycode_";

    /**
     * 创建验证码 保存session
     *
     * @param session session����
     * @return ��֤��
     */
    public static IVerifyCode createVerifyCode( HttpSession session )
    {
        IVerifyCode code = VerifyCodeBuilder.buildVerifyCode();
        session.setAttribute( VERIFYCODE_BINDKEY, code );
        return code;
    }

    /**
     * 获取验证码
     * @param session �Ự
     * @return IVerifyCode
     */
    public static IVerifyCode getCurrentVerifyCode( HttpSession session )
    {
        return ( IVerifyCode ) session.getAttribute( VERIFYCODE_BINDKEY );
    }

    /**
     * 验证
     *
     * @param toCheck
     * @param session
     * @return true
     */
    public static boolean checkVerifyCode( String toCheck, HttpSession session )
    {
        IVerifyCode code = getCurrentVerifyCode( session );
        return code != null && code.isMatch( toCheck );
    }
}
